<?php
Class UserModel extends CI_Model {
	
    function _construct() {
        parent::_construct();
        //$this->load->helper(array('form', 'url'));
        //$this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library('security');
        $this->load->library('tank_auth');
        $this->load->model('users/userModel');
        $this->load->model('TahapanusiaModel');
        $this->load->model('usiaUserModel');
        $this->load->database();
        $this->load->library('encrypt');
        $this->load->library('session');
        $this->load->helper('date');
    }
    
//	function validate()
//	{
//		$request = $_REQUEST['username'];
//		
//		//usleep(150000);
//		$query = mysql_query("SELECT * FROM users WHERE username ='$username'");
//		$result = mysql_num_rows($query);
//		if ($result == 0){
//		$valid = 'true';}
//		else{
//		$valid = 'false';
//		}
//		echo $valid;
//	}

    function __destruct() {
    }

    function login($username, $password) {
        $this -> db -> select('userid, username, gender, password');
        $this -> db -> from('users');
        $this -> db -> where('USERNAME',$username);
        $this -> db -> where('PASSWORD',$password);
        $this -> db -> limit(1);
//		$sql = "select * from users where username = $username and password = $password";

        $query = $this -> db -> get('');

        if($query -> num_rows() == 1) {
            return $query->result();
        }
        else {
            return false;
        }
    }
    
    function getUserId($username) {
    	$sql = "select USERID from users where USERNAME = '$username'";
		$data = $this->db->query($sql);
		return $data;
    }

    function add_users() {
        $USERNAME = $this->input->post('USERNAME');
        $PASSWORD = $this->input->post('PASSWORD');
        $GENDER = $this->input->post('GENDER');
        $EMAIL = $this->input->post('EMAIL');
        $BIRTHDATE = $this->input->post('BIRTHDATE');
        $data = array(
                'USERNAME' => $USERNAME,
                'PASSWORD' => $PASSWORD,
                'GENDER' => $GENDER,
                'EMAIL' => $EMAIL,
                'BIRTHDATE' => $BIRTHDATE
        );
        $this->db->insert('users', $data);
        $userid = $this->getUserId($USERNAME)->result();
        $usia = $this->setUsia($userid[0]->USERID)->result();
        $usiaid = $this->TahapanusiaModel->getUsia($usia[0]->umur)->result();
        $this->usiaUserModel->saveUsia($usiaid[0]->USIAID, $userid[0]->USERID);
    }
    
    function setUsia($userid) {
    	$sql = "select birthdate, year(curdate())- year(birthdate) as umur from users where userid = $userid";
		$data = $this->db->query($sql);
		return $data;
    }
    
//    function cekUsia($birthdate){
//    	$sql = "";
//    }

    function changePassword() {

    }
    
    
    
	public function registrasi($userData)
	{
		$message = "Saving Process is Succes!!";
		$status = TRUE;
            if($statuscheck == TRUE){
				$userData['password'] = $this->encrypt->encode($userData['password']);
				//insert data user ke tabel users
				$this->db->insert('users', $userData);
			}else{
				$message = "Registration failed, please check confirm password!!";
				$status = FALSE;
			}
			$userRegistration = array ('userData' => $userData, 'message' => $message, 'status' => $status);
		return $userRegistration;
	}	
}
?>
